package com.summer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.summer.common.OperateThreadLocal;
import com.summer.common.R;
import com.summer.entity.*;
import com.summer.service.AddressBookService;
import com.summer.service.OrderDetailService;
import com.summer.service.OrdersService;
import com.summer.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderDetailService orderDetailService;


    /**
     * 下单提交
     * @param orders 接收订单json信息
     * @return 返回字符串
     */
    @PostMapping("/submit")
    public R<String> orderMealSubmit(@RequestBody Orders orders){
        log.info("下单提交请求，订单orders={}",orders);
        // 下单要操作三个表，service创建方法
        ordersService.orderMealSubmit(orders);
        return R.success("下单成功");
    }

    /**
     * 根据id分页查询订单
     * @param page 当前页
     * @param pageSize 每页显示数
     * @return 返回Page<OrdersDTO>对象
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> getOrdersById(int page, int pageSize){
        log.info("根据id分页查询订单，page="+page+"，pageSize="+pageSize);
        //0. 数据准备
        Long userId = OperateThreadLocal.getLocalThreadAttribute();
        User user = userService.getById(userId);

        //1. 分页构造器, 条件构造器
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId, userId);
        lqw.orderByDesc(Orders::getOrderTime);
        //2. 调用service查询
        ordersService.page(ordersPage,lqw);

        //3. 封装Page<ordersDto>
        Page<OrdersDto> ordersDtoPage = new Page<>();
        // 对象深拷贝
        BeanUtils.copyProperties(ordersPage, ordersDtoPage,"records");
        List<OrdersDto> ordersDtoList = new ArrayList<>();
        int index = 0;
        for (Orders order : ordersPage.getRecords()) {
            OrdersDto ordersDto = new OrdersDto();
            //copy属性
            BeanUtils.copyProperties(order, ordersDto);
            //set其他属性
            ordersDto.setUserName(user.getName());
            ordersDto.setPhone(user.getPhone());
            ordersDto.setAddress(order.getAddress());
            ordersDto.setConsignee(order.getConsignee());
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, order.getNumber());
            ordersDto.setOrderDetails(orderDetailService.list(orderDetailLambdaQueryWrapper));
            ordersDtoList.add(ordersDto);
            index += 1;
        }
        ordersDtoPage.setRecords(ordersDtoList);
        //4. 返回page
        return R.success(ordersDtoPage);
    }

    /**
     * 管理端分页查询订单
     * @param page 当前页
     * @param pageSize 每页显示数
     * @return 返回Page对象
     */
    @GetMapping("/page")
    public R<Page<OrdersDto>> getAllOrders(int page, int pageSize){
        log.info("分页查询订单，page="+page+"，pageSize="+pageSize);
        //0. 数据准备

        //1. 分页构造器
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        //2. 调用service查询
        ordersService.page(ordersPage);
        //3. 封装Page<ordersDto>
        Page<OrdersDto> ordersDtoPage = new Page<>();
        // 对象深拷贝
        BeanUtils.copyProperties(ordersPage, ordersDtoPage,"records");
        // set其他属性
        List<OrdersDto> ordersDtoList = new ArrayList<>();
        int index = 0;
        for (Orders order : ordersPage.getRecords()) {
            // 根据订单userId属性获取一个user
            LambdaQueryWrapper<User> userLqw = new LambdaQueryWrapper<>();
            userLqw.eq(User::getId, order.getUserId());
            User user = userService.getOne(userLqw);

            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order, ordersDto); //copy属性
            // 设置其他属性
            ordersDto.setUserName(user.getName());
            ordersDto.setPhone(user.getPhone());
            ordersDto.setAddress(order.getAddress());
            ordersDto.setConsignee(order.getConsignee());
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, order.getNumber());
            ordersDto.setOrderDetails(orderDetailService.list(orderDetailLambdaQueryWrapper));
            ordersDtoList.add(ordersDto);
            index += 1;
        }
        ordersDtoPage.setRecords(ordersDtoList);
        //4. 返回page
        return R.success(ordersDtoPage);
    }
}
